#include "include.h"

RK_NS0340 rk_test;
uint8_t tmp2[2];
int16_t voltage;        //mv
float current;        //mA
float power;          //mW
float pow_self;
float battery;
INA226 ina226_data[2]              = {0};


uint8_t a = 0;
void test_task( void )
{

  /* led闪烁 */
  led_work_flicker();

  /* 检查系统是否要重启 */
  check_system_status();

  /* 获取服务器的数据 */
  get_server_data();

  /* 获取传感器的值 */
  get_sensor_vaule();

  /* 刷新串口屏数据 */
  refresh_screen_data();

  /* 显示屏测试 */
//  screen_test_xbc();
  //get sensor value
  if(rk.timer.get_sensor_value_timer >= Get_Sensor_Value_Timer_Max )
  {
//    GPIO_UART3_DE_SET;
//    GPIO_UART3_RE_SET;
//    GPIO_UART4_DE_SET;
//    GPIO_UART4_RE_SET;
//    GPIO_UART5_DE_SET;
//    GPIO_UART5_RE_SET;
//    uint8_t tx_buf[8] = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
//    HAL_UART_Transmit_DMA( &huart1, tx_buf, 8 );
//    HAL_UART_Transmit_DMA( &huart2, tx_buf, 8 );
//    HAL_UART_Transmit_DMA( &huart3, tx_buf, 8 );
//    HAL_UART_Transmit_DMA( &huart4, tx_buf, 8 );
//    HAL_UART_Transmit_DMA( &huart5, tx_buf, 8 );
//    HAL_UART_Transmit_DMA( &huart6, tx_buf, 8 );


    if(a == 0)
    {
//      GPIO_MCU_IO3_SET;
//      GPIO_UART5_DE_SET;
//      GPIO_UART5_RE_SET;
//      HAL_Delay(10);
//
//      write_rs485_ATMT_two_culumn_motor_value( 0x01, 0x06, 0x00e1, 0x01, 0x07 );
//      HAL_Delay(10);
//      GPIO_UART5_DE_RESET;
//      GPIO_UART5_RE_RESET;
//      rk.flag.upload = true;
//
//      //注册
//      upload_package = upload_login_package;
////      //心跳包
////      upload_package = upload_heartbeat_package;
//
////      //切换模式
////      upload_package = upload_switch_mode_package;
//
////      //报警
////      rk.upload.alarm_status = Alarming;
////      rk.upload.alarm_type   = Gate_Positon_Overlimit;
////      upload_package = upload_alarm_package;
//
////      //故障
////      rk.upload.malfunction_status = Malfunctioning;
////      rk.upload.malfunction_type   = Water_Level_Downstream_Link;
//
////      //业务数据主动上报
////      rk.flag.func_data_initiative_update = true;
////      upload_package = upload_function_data_package;
//
////      //工况数据主动上报
////      rk.flag.THA_data_initiative_update = true;
////      upload_package = upload_THA_data_package;
////
//      //工况数据主动上报
//

//      upload_package_init();
//
//      upload_package_crc_end( upload_package() );
//
//      EC20_send_data( rk.upload.txbuf, rk.upload.txbuf[Package_Len_Pos]+Package_Aside_From_User_Data );

      a++;
    }

    /* encoder */
//    get_ATMT_ABSencoder_value();

    /* flowmeter */
//    get_XYflowmeter_value( XY_BOTH_GET_SD );

    /* ds18b20 */
    rk.ds18b20.temperature = DS18B20_Read_Temperature();

    /* INA226 */
    get_ina226_power( &hi2c1, IIC_ADDR(Dev_Addr_Ina226_P24,READ), &rk.ina226 );
    rk.timer.get_sensor_value_timer = 0;
  }

  if( rk.flag.upload == true )
  {
    upload_package_init();

    upload_package_crc_end( upload_package() );

    EC20_send_data( rk.upload.txbuf, rk.upload.txbuf[Package_Len_Pos]+Package_Aside_From_User_Data );

    if(rk.upload.confirm_type == AFN_Set_Device_Restart )
    {
      rk.flag.system_restart = true;
    }
    rk.flag.upload = false;
  }

//  //control motor
//  if(rk.timer.set_motor_dutyfactor_timer >= Control_Motor_Timer_Max )
//  {
//    set_ATMTmotor_pwm_dutyfactor( MOTOR_PWM_Dutyfactor_High );
//
//    rk.timer.set_motor_dutyfactor_timer = 0;
//  }
//
//  //revice
  if( rk.flag.recv_XY_ABCencoder )
  {
    recv_XY_ABSencoder_package_handle();
    rk.flag.recv_XY_ABCencoder = false;
  }
  if( rk.flag.recv_XY_flowmeter )
  {
    recv_XYflowmeter_package_handle();
    rk.flag.recv_XY_flowmeter = false;
  }

  if( rk.flag.recv_ATMT_flowmeter )
  {
    recv_ATMTflowmeter_package_handle();
    rk.flag.recv_ATMT_flowmeter = false;
  }
  if( rk.flag.recv_ATMT_ABSencoder )
  {
    recv_ATMT_ABSencoder_package_handle();
    rk.flag.recv_ATMT_ABSencoder = false;
  }

//  w25_test();
  /* encoder */
//  uchar tx_buf[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
//  HAL_UART_Transmit_DMA( &huart1, tx_buf, 0x08 );
//

}
